import {generateSuit} from "@/utils/artifact_utils";

export default class socketService {
    static instance = null

    static get Instance() {
        if (this.instance == null) {
            this.instance = new socketService()
        }
        return this.instance
    }

    ws = null
    callBackMapping = {}
    registerCallBack(socketType, callback) {
        console.log("{} register callback function", socketType)
        this.callBackMapping[socketType] = callback
    }
    connect() {
        this.ws = new WebSocket("ws://localhost:11020/ys/artifact");
        this.ws.onopen = function () {
            console.log(1)
        }
        this.ws.onclose = function () {
            console.log(2)
        }
        this.ws.onmessage = msg => {
            const recvData = JSON.parse(msg.data)

            let total = {}
            const flowerList = recvData.flowerList;
            const plumList = recvData.plumList;
            const eonList = recvData.eonList;
            const gobletList = recvData.gobletList;
            const circletList = recvData.circletList;

            flowerList.sort((a,b)=>b[22]-a[22])
            plumList.sort((a, b)=>b[22]-a[22]);
            eonList.sort((a,b)=>b[22]-a[22])
            gobletList.sort((a,b)=>b[22]-a[22])
            circletList.sort((a,b)=>b[22]-a[22])

            total.flowerList = generateSuit(flowerList, 1000,true)
            total.plumList = generateSuit(plumList, 1000,true)
            total.eonList = generateSuit(eonList, 1000,true)
            total.gobletList = generateSuit(gobletList, 1000,true)
            total.circletList = generateSuit(circletList, 1000,true)

            this.callBackMapping['artifact'].call(this,total);
        }
    }
}
